package com.example.knowman.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.knowman.ReturnMessage.*;
import com.example.knowman.dao.Log4MainKeys;
import com.example.knowman.dao.knowCategoryWithStaffname;
import com.example.knowman.dao.knowCategoryWithStaffnameAndChild;
import com.example.knowman.dao.log4Ex;
import com.example.knowman.entity.knowCategory;
import com.example.knowman.entity.log4operate;
import com.example.knowman.service.IKnowCategoryService;
import com.example.knowman.service.ILog4operateService;
import com.example.knowman.service.IStaffListService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/log4operate")
public class Log4operateController {
    private final ILog4operateService iLog4operateService;

    private final IStaffListService iStaffListService;


    public Log4operateController(ILog4operateService iLog4operateService,IStaffListService iStaffListService) {
        this.iLog4operateService = iLog4operateService;
        this.iStaffListService = iStaffListService;
    }

    @GetMapping("getAll")
    public ResponseEntity<MessageBackLog4> getEasyClass(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                        @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                        @RequestParam(required = false) String operator,
                                                        @RequestParam(required = false) String createtime,
                                                        @RequestParam Integer TimeType) {

        MessageBackLog4 mes = new MessageBackLog4();

        //获得员工Map<工号，名字>
        Map<String, String> StaffMap = iStaffListService.getIdAndName();

        //获得日志表分页信息
        IPage<log4operate> p = iLog4operateService.getAll(pageNum,pageSize,operator,createtime,TimeType);

        IPage<log4Ex> res = new Page<>(pageNum, pageSize);

        List<log4Ex> records = new ArrayList<>();
        for (log4operate item:p.getRecords()){
            log4Ex  temp = new log4Ex();
            temp.setCreatetime(item.getCreatetime());
            temp.setOperator(item.getOperator());
            temp.setContent(item.getContent());
            temp.setIpAddress(item.getIpAddress());
            temp.setRecId(item.getRecId());

            temp.setOperatorName(StaffMap.get(item.getOperator()));

            records.add(temp);
        }

        res.setTotal(p.getTotal());
        res.setRecords(records);
        mes.setCode(200);
        mes.setTip("查询成功");
        mes.setList(res);
        return ResponseEntity.ok(mes);
    }


    //批量删除 可能主键到时候有问题要换种方式去删除
    @PostMapping("DeleteByRecIds")
//    public ResponseEntity<Message> DeleteByRecIds(@RequestBody List<Integer> recIds) {
    public ResponseEntity<Message> DeleteByRecIds(@RequestBody Log4MainKeys requestBody) {
//        System.out.println(recIds);
        List<Integer> recIds = requestBody.getRecIds();
        Message mes  = new Message();
        if(iLog4operateService.removeByIds(recIds)){
            mes.setCode(200);
            mes.setTip("删除成功");
        }
        else{
            mes.setCode(100);
            mes.setTip("删除失败");

        }


        if (mes.getCode() == 200) {
            return ResponseEntity.ok(mes);
        } else {
            return ResponseEntity.badRequest().body(mes);
        }
    }


    //insert测试
    @GetMapping("inse")
    public ResponseEntity<Message> inse(@RequestParam String operator,@RequestParam String content,@RequestParam String ipAddress) {


        Message mes  = new Message();
        if(iLog4operateService.insertSingle(operator,content,ipAddress)){
            mes.setCode(200);
            mes.setTip("新增成功");
        }
        else{
            mes.setCode(100);
            mes.setTip("新增失败");

        }


        if (mes.getCode() == 200) {
            return ResponseEntity.ok(mes);
        } else {
            return ResponseEntity.badRequest().body(mes);
        }
    }
}
